package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.ProgramDetailBean;
import com.adgox.tiantianbiting.bean.ShareUrlBean;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.redpacket.share.share_detail.ShareDetailActivity;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.adgox.tiantianbiting.utils.TimeUtils;
import com.adgox.tiantianbiting.utils.WebviewActivity;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2019/3/11.
 */

public class MyShareAdapter extends RecyclerView.Adapter<MyShareAdapter.ViewHolder> implements View.OnClickListener {
    private List<ProgramDetailBean> datas;
    private SparseArray<CountDownTimer> countArr;

    public MyShareAdapter(List<ProgramDetailBean> datas) {
        this.datas = datas;
        countArr = new SparseArray<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_share_my, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ProgramDetailBean bean = datas.get(position);
        if (holder.countDownTimer != null) {
            holder.countDownTimer.cancel();
        }
        long time = bean.getShareCreateTime() + bean.getSurvivalTime() - System.currentTimeMillis();
        if (time > 0) {
            holder.countDownTimer = new CountDownTimer(time, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int second = (int) (millisUntilFinished / 1000);
                    holder.tvStatus.setText(TimeUtils.SecondToHms(second) + "后结束");
                }

                @Override
                public void onFinish() {
                    holder.tvStatus.setText("已结束");
                    holder.tvShare.setText("失败");
                    holder.tvShare.setBackgroundResource(R.drawable.bg_share_failed);
                }
            }.start();
            countArr.put(holder.tvStatus.hashCode(),holder.countDownTimer);
        }

        holder.tvPosition.setText((position + 1) + "");
        holder.tvBookName.setText(bean.getTitle());
        Glide.with(holder.itemView.getContext()).load(bean.getShareImageUrl()).into(holder.ivBook);
        if ("0".equals(bean.getShareStatus())) {
            //分享成功
            holder.tvStatus.setText("已结束");
            holder.tvShare.setText("免费听");
            holder.tvShare.setBackgroundResource(R.drawable.img_share_button);
        } else if ("2".equals(bean.getShareStatus())) {
            //分享失败
            holder.tvStatus.setText("已结束");
            holder.tvShare.setText("失败");
            holder.tvShare.setBackgroundResource(R.drawable.bg_share_failed);
        } else if ("1".equals(bean.getShareStatus())) {
            //分享中
            holder.tvShare.setText("继续分享");
            holder.tvShare.setBackgroundResource(R.drawable.bg_share_cotinue);
        }
        holder.tvShareDetail.setTag(position);
        holder.tvShareDetail.setOnClickListener(this);
        holder.tvShare.setTag(position);
        holder.tvShare.setOnClickListener(this);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
    }

    public void cancelAllCDT(){
        if (countArr == null){
            return;
        }
        for (int i = 0; i<countArr.size();i++){
            CountDownTimer cdt = countArr.get(countArr.keyAt(i));
            if (cdt != null){
                cdt.cancel();
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void addDatas(List<ProgramDetailBean> beans) {
        datas.addAll(beans);
        notifyDataSetChanged();
    }

    public void clear() {
        datas.clear();
        notifyDataSetChanged();
    }

    public enum ViewName {
        SHARE,
        DETAIL,
        ITEM
    }

    public interface onItemClickListener {
        void onItemClick(View view, ViewName viewName, int position);
    }

    public onItemClickListener listener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (v.getId() == R.id.tv_share_detail) {
            listener.onItemClick(v, ViewName.DETAIL, position);
        } else if (v.getId() == R.id.tv_share_status) {
            listener.onItemClick(v, ViewName.SHARE, position);
        } else {
            listener.onItemClick(v, ViewName.ITEM, position);
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_position)
        TextView tvPosition;
        @BindView(R.id.iv_book)
        ImageView ivBook;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_share_status)
        TextView tvShare;
        @BindView(R.id.tv_share_detail)
        TextView tvShareDetail;
        CountDownTimer countDownTimer;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
